import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/#{database}")
public class #{Database}Controller{
    @Autowired
    #{Database}ServiceImp #{database}Service;

    @Autowired
    #{Database}Mapper #{database}Mapper;

    //查询数据总条数
    @RequestMapping("total")
    public CommonResult selectLength() {
        return new CommonResult(#{database}Service.count());
    }

    //查询
    @GetMapping("/select")
    public CommonResult select(
        @RequestParam(name = "@{field.name}", required = false, defaultValue = "null") @{field.type} @{field.name},@{field}
    ){
        QueryWrapper<#{Database}> wrapper = new QueryWrapper<>();
        if (!@{field.name}.equals("null")) wrapper.like("@{field.name}", @{field.name});@{field}
        return new CommonResult(#{database}Mapper.selectList(wrapper));
    }

    // 实现分页查询
    @GetMapping("list")
    public CommonResult selectPage(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size
    ) {
        Page<#{Database}> page = new Page<>(current, size);
        #{database}Mapper.selectPage(page, null);
        return new CommonResult(page.getRecords());
    }

    // 实现删除
    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestParam("id")String id){
        return CommonResult.succ(#{database}Mapper.deleteById(id));
    }

    // 实现插入
    @PostMapping("/insert")
    public CommonResult addMenu(@RequestBody #{Database} #{database}){
        return CommonResult.succ(#{database}Mapper.insert(#{database}));
    }

    // 实现更新
    @PostMapping("/update")
    public CommonResult updateMenu(@RequestBody #{Database} #{database}){
        return CommonResult.succ(#{database}Mapper.updateById(#{database}));
    }

}